from bs4 import BeautifulSoup #网页解析，获取数据
import re  #正则表达式，进行文字匹配
import urllib.request,urllib.error #制定URL，获取网页数据
import  xlwt #进行Excel操作
import json
def main():
    baseurl = "https://www.cingta.com/school/api/name_uni_list/"
    datalist = getData(baseurl)
    savepath = 'school.xls'
    saveData(savepath)
def getData(baseurl):
    datalist = []
    html = askURL(baseurl)
    data = json.loads(html)
    datalist = data['data']
    li = datalist['list']
    for i in li:
        print(i)
    soup = BeautifulSoup(html, 'html.parser')
   # print(soup)
    return datalist

def askURL(url):
    head = {
        "User - Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3870.400 QQBrowser/10.8.4405.400"
    }
    request = urllib.request.Request(url, headers=head)
    html = ""
    try:
        response = urllib.request.urlopen(request)
        html = response.read().decode("GBK")
        #html = response.text.encode('iso-8859-1').deconde('gbk')
    except urllib.error.URLError as e:
        if hasattr(e, "code"):
            print(e.code)
        if hasattr(e, "reason"):
            print(e.reason)
    return html

def saveData(savepath):
    print('111')

if __name__ == '__main__':
    main()